package com.littcore.saap.util;

import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class DateAdapter implements JsonDeserializer<Date> ,JsonSerializer<Date>{
 
  private final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  
  public JsonElement serialize(Date src, Type arg1, JsonSerializationContext arg2) {   
      String dateFormatAsString = df.format(new Date(src.getTime()));   
      return new JsonPrimitive(dateFormatAsString);   
  }   
  
  
  public Date deserialize(JsonElement arg0, Type arg1,
      JsonDeserializationContext arg2) throws JsonParseException {
    try {
      return df.parse(arg0.getAsString());
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return null;
  }
}